package com.shengruan.admin.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.shengruan.admin.vo.ProductClass;
import com.shengruan.framework.jdbc.BaseJdbc;
import com.shengruan.framework.jdbc.BeanHandler;
import com.shengruan.framework.jdbc.BeanListHandler;

public class ProductClassJdbc  extends BaseJdbc{
	protected static Log log = LogFactory.getLog(ProductClassJdbc.class);
	/**
	 * 保存
	 */
	public void save(ProductClass productClass)
	{
		Connection conn = this.getConnection();
		String sql = "insert into product_class (" +
		"name,createTime)" +
		" values(?,?)";
		try {
			this.execute(conn, sql, productClass.getName(),productClass.getCreateTime());
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		
		
	}
	
	/**
	 * 更新
	 */
	public void update(ProductClass productClass)
	{

		Connection conn = this.getConnection();
		String sql = "update product_class set name=?, createTime=? where id=?";
		try {
			this.execute(conn, sql, productClass.getName(),productClass.getCreateTime(), productClass.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
	}
	
	/***
	 * 根据ID删除
	 */
	public void deleteById(int id)
	{
		Connection conn = this.getConnection();
		String sql = "delete from product_class where id = ? ";
		try {
			this.execute(conn, sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
	}
	
	
	/***
	 * 获得列表
	 * @return
	 */
	public  List<ProductClass> getList()
	{
		List<ProductClass> productClassList = new ArrayList<ProductClass>();
		Connection conn = this.getConnection();
		String sql = "select * from  product_class order by createTime desc";
		try {
			productClassList = this.query(conn, sql,new BeanListHandler<ProductClass>(ProductClass.class));
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return productClassList;
	}


    public  List<ProductClass> getList(String name)
	{
		List<ProductClass> productClassList = new ArrayList<ProductClass>();
		Connection conn = this.getConnection();
        StringBuffer sb = new StringBuffer("select * from  product_class where 1=1 ");
        if(StringUtils.isNotEmpty(name)) sb.append(" and name like '%"+name+"%'");
        sb.append(" order by createTime desc");

		try {
			productClassList = this.query(conn, sb.toString(),new BeanListHandler<ProductClass>(ProductClass.class));
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return productClassList;
	}
	
	
	/***
	 * 根据用ID获得
	 */
	public ProductClass getById(int id)
	{
		Connection conn = this.getConnection();
		
		ProductClass productClass =null;
		String sql = "select * from product_class where id = ?";
		try {
			productClass = this.query(conn, sql,new BeanHandler<ProductClass>(ProductClass.class), id);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return productClass;
	}
	
}
